package com.xunyun.kotlinwc.main.viewmodel

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.xunyun.kotlinwc.base.BaseViewModel
import com.xunyun.kotlinwc.main.datasource.ProjectDataSourceFactory
import com.xunyun.kotlinwc.main.model.Project
import com.xunyun.kotlinwc.utils.paging.PagingConfig

/**
 * @author gl
 * @date 2021/9/14
 * @desc
 */
class ProjectViewModel : BaseViewModel() {

    lateinit var mListLiveData: LiveData<PagedList<Project>>

    /**
     * 下拉刷新(从第一页重新加载)
     */
    fun refresh() {
        mListLiveData.value!!.dataSource.invalidate()
    }

    /**
     * 绑定LifecycleOwner 并设置LiveData<PagedList<Project>>
     */
    fun bindLifecycleOwner(lifecycleOwner: LifecycleOwner, cid: Int) {
        LivePagedListBuilder(
            ProjectDataSourceFactory(lifecycleOwner, infoMsg, cid),
            PagingConfig.pageConfig()
        ).build()
            .also {
                this.mListLiveData = it
            };
    }

}